<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')" prop="dealId">
            <MainBodySelect
              v-model="dealId"
              parameters="deal"
              :disabled="dealId ? true : false"
              @change="dealIdChangeHandle"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目负责人')" prop="personCharge">
            <UserSelect v-model="form.personCharge" :placeholder="$t('项目负责人')" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('评级时间')" prop="ratingDate">
            <DatePicker v-model="form.ratingDate" :placeholder="$t('评级时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目状态')" prop="dealStatus">
            <dictSelect
              v-model="form.dealStatus"
              :clearable="true"
              dictionary="deal_status"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('经营状况')" prop="businessScope">
            <el-input v-model="form.businessScope" :placeholder="$t('经营状况')" show-word-limit maxlength="200" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资金额') + $t('金额单位')" prop="amountInvestmentFact">
            <Money v-model="form.amountInvestmentFact" unit="w" :placeholder="$t('投资金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('持股比例(%)')" prop="latestShareholdingRatio">
            <Money v-model="form.latestShareholdingRatio" :placeholder="$t('持股比例(%)')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('当期评级')" prop="currentRating">
            <el-select v-model="form.currentRating" filterable :placeholder="$t('当期评级')">
              <el-option
                v-for="dict in currentRatingOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('评级人员')" prop="ratingStaff">
            <UserSelect v-model="form.ratingStaff" :placeholder="$t('评级人员')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('项目简介')" prop="dealIntroduction">
            <el-input v-model="form.dealIntroduction" type="textarea" :placeholder="$t('项目简介')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('评级描述')" prop="ratingDescribe">
            <el-input v-model="form.ratingDescribe" type="textarea" :placeholder="$t('评级描述')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="area-header">
        <span class="area-header-title">{{ $t('评级指标') }}</span>
      </div>
      <el-table
        :data="form.dealAssetRatingIndexList"
        border
        class="nowrapTable formTable"
        :header-cell-style="{'text-align':'center'}"
      >
        <el-table-column :label="$t('序号')" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('评级指标')" prop="indexName" show-overflow-tooltip min-width="500px">
          <template slot-scope="scope">
            <span style="text-align: left"> {{ scope.row.indexName }} </span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('说明')" min-width="500px">
          <template slot-scope="scope">
            <el-form-item :prop="'dealAssetRatingIndexList.' + scope.$index + '.remark'">
              <el-input v-model="scope.row.remark" type="textarea" :placeholder="$t('说明')" maxlength="2000" :autosize="{ minRows: 1 }" show-word-limit />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('单项评级结果')" min-width="150px">
          <template slot="header" slot-scope="{}">
            <span>{{ $t('单项评级结果') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'dealAssetRatingIndexList.' + scope.$index + '.singleRatingResult'" :rules="indexRules.singleRatingResult">
              <dict-select
                v-model="scope.row.singleRatingResult"
                dictionary="current_rating_type"
                :placeholder="$t('单项评级结果')"
                @change="getCurrentRating"
              />
            </el-form-item>
          </template>
        </el-table-column>
      </el-table>
      <br>
      <!-- 附件列表 -->
      <FileTable :list.sync="form.fileList" has-permi="deal:rating" data-type="deal_asset_rating" :business-id="autoBusinessId($route.query.id, form)" :btn-show="!otherBtnHide" />
      <br>
      <div class="area-header">
        <span class="area-header-title">{{ $t('评级标准') }}</span>
      </div>

      <el-table
        :data="form.dealConfigRatingStandardList"
        border
        tooltip-effect="darkTable"
        :header-cell-style="{'text-align':'center'}"
      >
        <el-table-column :label="$t('评级结果')" align="center" prop="ratingType" :formatter="ratingTypeFormat" width="150" />
        <el-table-column :label="$t('评级标准')" class-name="dealContractPlan">
          <template slot-scope="scope">
            <div class="ratingPlan">
              <div v-for="(value,index) in scope.row.children" :key="index">
                <pre style="font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, SimSun, sans-serif;"> {{ value.ratingStandard }} </pre>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>

    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <save-btn has-permi="deal:rating" />
    </div>
  </div>
</template>

<script>
    import { saveDealAssetRating, getDealAssetRating, findDealById, addConFigIndex } from '@/api/deal/rating/dealAssetRating'
    import MainBodySelect from '@/components/MainBodySelect/index'
    export default {
      components: {
        MainBodySelect
      },
        data() {
            return {
                dealId: this.$route.query.id,
                // 当期评级字典
                currentRatingOptions: [], // 表单参数
                form: {
                  dealAssetRatingIndexList: [],
                  dealConfigRatingStandardList: []
                },
                // 遮罩层
                loading: false,
              // 是否隐藏其它按钮
              otherBtnHide: false,
              ratingTypeOptions: [],
                // 表单校验
                rules: {
                  dealId: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], ratingDate: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], currentRating: [
                    { required: true, message: this.$t('必填信息'), trigger: 'change' }
                  ], ratingStaff: [
                    { required: true, message: this.$t('必填信息'), trigger: 'change' }
                  ], dealStatus: [
                    { required: true, message: this.$t('必填信息'), trigger: 'change' }
                  ], businessScope: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], amountInvestmentFact: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], latestShareholdingRatio: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], dealIntroduction: [
                    { required: false, message: this.$t('必填信息'), trigger: 'blur' }
                  ]
                },
              indexRules: {
                singleRatingResult: [{ required: false, message: this.$t('必填信息'), trigger: 'change' }]
              }
            }
        },
        created() {
            this.getDicts('current_rating_type').then(response => {
                this.currentRatingOptions = response.data
            })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                  getDealAssetRating(data.id).then(response => {
                    this.form = response.data
                    console.log(this.form, 'this.formthis.form')
                      findDealById(this.form.dealId).then(res => {
                        // 历史数据带出项目数据
                        if (!this.form.dealStatus) {
                        this.form.personCharge = res.data.personCharge
                        this.form.dealStatus = res.data.dealStatus
                        this.form.dealIntroduction = res.data.dealIntroduction
                        this.form.amountInvestmentFact = res.data.amountInvestmentFact
                          this.form.latestShareholdingRatio = res.data.latestShareholdingRatio
                        }
                        // 修改时只展示评级标准
                        this.addConFigIndex(res.data.dealType, false)
                      })
                    this.loading = false
                  })
                } else {
                    this.reset()
                    if (this.dealId) {
                      // 带出项目数据
                        findDealById(this.dealId).then(res => {
                          this.form.personCharge = res.data.personCharge
                          this.form.dealStatus = res.data.dealStatus
                          this.form.dealIntroduction = res.data.dealIntroduction
                          this.form.amountInvestmentFact = res.data.amountInvestmentFact
                          this.form.latestShareholdingRatio = res.data.latestShareholdingRatio
                          this.addConFigIndex(res.data.dealType, true)
                        })
                    } else {
                      this.loading = false
                    }
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                dealId: this.dealId,
                personCharge: undefined,
                ratingDate: undefined,
                currentRating: undefined,
                ratingStaff: this.$store.state.user.userId,
                ratingDescribe: undefined,
                  dealIntroduction: undefined,
                  latestShareholdingRatio: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined,
                fileList: []
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealAssetRating(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
          // 获取配置模板数据
          addConFigIndex(type, isIndex) {
            addConFigIndex(type).then(config => {
              if (isIndex === true) {
                this.form.dealAssetRatingIndexList = config.data.dealAssetRatingIndexList
              }
              this.form.dealConfigRatingStandardList = config.data.dealConfigRatingStandardList
              this.loading = false
            })
          },
          dealIdChangeHandle(val) {
              findDealById(val).then(res => {
                this.form.personCharge = res.data.personCharge
                this.form.dealStatus = res.data.dealStatus
                this.form.dealIntroduction = res.data.dealIntroduction
                this.form.amountInvestmentFact = res.data.amountInvestmentFact
                this.form.latestShareholdingRatio = res.data.latestShareholdingRatio
              })
          },
          // 评级结果字典翻译
          ratingTypeFormat(row) {
            return this.selectDictLabel(this.currentRatingOptions, row.ratingType)
          },
          // 根据评级指标结果判断 当期评级
          getCurrentRating() {
              var rating = []
              this.form.dealAssetRatingIndexList.forEach((item) => {
                if (item.singleRatingResult) {
                  rating.push(item.singleRatingResult)
                }
              })
            if (rating !== []) {
              if (rating.toString().includes('loss')) {
                this.form.currentRating = 'loss'
              } else if (rating.toString().includes('secondary')) {
                this.form.currentRating = 'secondary'
              } else if (rating.toString().includes('suspicious')) {
                this.form.currentRating = 'suspicious'
              } else if (rating.toString().includes('attention')) {
                this.form.currentRating = 'attention'
              } else if (rating.toString().includes('normal')) {
                this.form.currentRating = 'normal'
              }
            }
          }
        }
    }
</script>

<style lang="scss" scoped>
  .ratingPlan{
    div{
      overflow: hidden;
      text-overflow: ellipsis;
      border-bottom: 1px solid #dfe6ec!important;
      /*padding: 0 10px;*/
      &:last-child{
        border-bottom: 0!important;
      }
    }
  }
</style>
